/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Proveedores;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author alrojas
 */
public final class IEditar extends JFrame{
    
    private JLabel razonSocialLab; 
    private JLabel rucLab;       
    private JLabel direccionLab;
    private JLabel telefonoLab;
    
    private JTextField razonSocialTex; 
    private JTextField rucTex;       
    private JTextField direccionTex;
    private JTextField telefonoTex;
    
    private JButton guardarBut;
    private JButton descartarBut;        

    public IEditar(IPrincipal padre, Proveedor proveedor) throws HeadlessException, ParseException{
        
        this.setLayout(new GridBagLayout());
        this.setTitle("Editar Proveedor");
        
        inicializarComponentes(proveedor);
        inicializarListeners(padre,proveedor);
        
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }
    
    public void inicializarComponentes(Proveedor proveedor) throws ParseException{
        
        GridBagConstraints limites = new GridBagConstraints();
        limites.insets = new Insets(2, 2, 2, 2);
        
        limites.fill = GridBagConstraints.BOTH;
        
        razonSocialLab = new JLabel("Razón Social"); 
        limites.gridx = 0; limites.gridy = 0;
        add(razonSocialLab,limites);
        
        rucLab = new JLabel("R.U.C.");      
        limites.gridy = 1;
        add(rucLab,limites);
        
        direccionLab = new JLabel("Dirección");
        limites.gridy = 2;
        add(direccionLab,limites);
        
        telefonoLab = new JLabel("Teléfono");
        limites.gridy = 3;
        add(telefonoLab,limites);
               
        razonSocialTex = new JTextField(20); razonSocialTex.setText(proveedor.getRazonSocial());
        limites.gridx = 1; limites.gridy = 0;
        add(razonSocialTex,limites);
        
        rucTex = new JTextField(10); rucTex.setText(proveedor.getRUC());      
        limites.gridy = 1;
        add(rucTex,limites);
        
        direccionTex = new JTextField(10); direccionTex.setText(proveedor.getDireccion());
        limites.gridy = 2;
        add(direccionTex,limites);
        
        telefonoTex = new JTextField(10); telefonoTex.setText(proveedor.getTelefono());
        limites.gridy = 3;
        add(telefonoTex,limites);
         
        JPanel panelBotones = new JPanel(new FlowLayout());
        guardarBut = new JButton("Guardar", new ImageIcon("figuras/save32.png"));
        guardarBut.setHorizontalTextPosition(SwingConstants.CENTER);
        guardarBut.setVerticalTextPosition(SwingConstants.BOTTOM) ;
        guardarBut.setVerticalAlignment(SwingConstants.CENTER);
        descartarBut = new JButton("Descartar", new ImageIcon("figuras/trash32.png"));
        descartarBut.setHorizontalTextPosition(SwingConstants.CENTER);
        descartarBut.setVerticalTextPosition(SwingConstants.BOTTOM) ;
        descartarBut.setVerticalAlignment(SwingConstants.CENTER);
        limites.gridy = 8;
        panelBotones.add(guardarBut); panelBotones.add(descartarBut);
        add(panelBotones,limites);

    }
    
    public void inicializarListeners(final IPrincipal padre,final Proveedor proveedor){
        guardarBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
                    proveedor.setRazonSocial(razonSocialTex.getText());
                    proveedor.setRUC(rucTex.getText());
                    proveedor.setDireccion(direccionTex.getText());
                    proveedor.setTelefono(telefonoTex.getText());
                    
                    boolean resultado = MProveedor.editar(proveedor);
                    if (resultado == false){
                        JOptionPane.showMessageDialog(null,"Proveedor modificado correctamente!","Operación Exitosa!",JOptionPane.INFORMATION_MESSAGE);
                        setVisible(false); 
                        padre.actualizar();
                    }
                } catch (SQLException ex) {
                    Logger.getLogger(IEditar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(IEditar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        
        descartarBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
}
